package com.uphone.kingmall.activity.personal;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.DialogUtil;
import com.uphone.kingmall.utils.HttpParamsUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentData;
import com.uphone.kingmall.view.SubmitButton0;
import com.uphone.kingmall.view.dialog.RadishDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail :
 *     time   : 2018/07/31
 *     desc   :余额转账
 *     version: 1.0
 * </pre>
 */
public class TransferAccountsActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_recharge)
    EditText etRecharge;
    @BindView(R.id.btn_queren)
    SubmitButton0 btnQueren;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @IntentData
    private int type = 0;
    private RadishDialog payDialog;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_transfer_accounts;
    }

    @Override
    public void initView() {
        setStatusBar();
        btnQueren.setRelaViews(etAccount, etRecharge);
    }

    @Override
    public void initData() {
        if (type == 0) {
            text1.setText("转账金额");
            text2.setVisibility(View.VISIBLE);
            etRecharge.setHint("请输入金额");
        } else {
            text1.setText("转账金币");
            text2.setVisibility(View.GONE);
            etRecharge.setHint("请输入金币");
        }
    }

    @OnClick({R.id.iv_back, R.id.btn_queren})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.btn_queren:
                if (TextUtils.isEmpty(etAccount.getText().toString())) {
                    showShortToast("请输入对方账户");
                    return;
                }
                if (TextUtils.isEmpty(etRecharge.getText().toString())) {
                    showShortToast("充值金额不能为空");
                    return;
                }
                KeyboardUtils.hideSoftInput(this);

                payDialog = DialogUtil.showDialogWalletPay(TransferAccountsActivity.this, new DialogUtil.OnWalletPayDialogCallBack() {
                    @Override
                    public void callBack(RadishDialog dialog, View view, String psd) {
                        HttpParams params = HttpParamsUtils.getInstance()
                                .put("money", etRecharge.getText().toString().trim())
                                .put("paypass", psd).getParams();
                        String url = "";
                        if (type == 0) {
                            params.put("touser", etAccount.getText().toString().trim());
                            url = MyUrl.yueZhuanZhang;
                        } else if (type == 1) {
                            params.put("toUser", etAccount.getText().toString().trim());
                            url = MyUrl.jifZhuanZhang;
                        }
                        OkGoUtils.progressRequest(url, TransferAccountsActivity.this, params, new onNormalRequestListener() {
                            @Override
                            public void onSuccess(String response, int code) {
                                if (code == 0) {

                                    if (payDialog != null && payDialog.isShowing()) {
                                        payDialog.dismiss();
                                    }
                                    ToastUtils.showShortToast(TransferAccountsActivity.this, "转账成功！");
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            finish();
                                        }
                                    }, 500);
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {

                            }
                        });
                    }
                });
//                IntentUtils.getInstance()
//                        .with(this, WalletPayPassActivity.class)
//                        .putString(ConstansUtils.PARA, WalletPayPassActivity.Pay)
//                        .putString("clazz", getClass().getName())
//                        .start();
                break;
        }
    }

    @Override
    public void onPayPsdSuccess(String passWord) {
        super.onPayPsdSuccess(passWord);
        HttpParams params = HttpParamsUtils.getInstance()
                .put("money", etRecharge.getText().toString().trim())
                .put("paypass", passWord).getParams();
        if (type == 0) {
            params.put("touser", etAccount.getText().toString().trim());
            CommonUtil.submitNetAndFinish(this, MyUrl.yueZhuanZhang, params, "转账成功！");
        } else if (type == 1) {
            params.put("toUser", etAccount.getText().toString().trim());
            CommonUtil.submitNetAndFinish(this, MyUrl.jifZhuanZhang, params, "转账成功！");
        }
    }
}
